const app = getApp()
var fakedata=require("../../static/js/fakedata");
Page({
    data: {
        userInfo: {},
        text:{},
        hasUserInfo: false,
        name:"哈哈",
        canIUse: swan.canIUse('button.open-type.getUserInfo'),
        fullscreen:false,
        display:true,
        position:"none",
        // 小程序tab列表框所用数据
        topmean: [{
            name:'all',
            label:'全部商品'
        },{
            name: 'digital',
            label: '数码产品'
        }, {
            name: 'appliances',
            label: '家用电器'
        }, {
            name: 'oldbook',
            label: '二手图书'
        }, {
            name: 'clothes',
            label: '服饰鞋帽'
        }, {
            name: 'sports',
            label: '运动户外'
        }, {
            name: 'lifegoods',
            label: '生活百货'
        }, {
            name: 'artifact',
            label: '宿舍神器'
        }],
        // 小程序商品列表：
        commodity:{},
        selectedMenu:"all",
        displayList:[], //首页展示的数据结果
        // 地图功能相关数据
        latitude: '',
        longitude: '',
        markers: [],
        map_rate:0.7, // 地图占屏幕高度的比例
        // 系统相关信息
        systemInfo:{}
    },
    // 设置搜索相关函数
    handleSearch(e){
        var key=e["value"];
        swan.request({
            url: fakedata.urls.search,
            header: {
                'Content-Type': 'application/json',
                'Developer-Server-Domain': fakedata.host,
                'X-SP-Accept-Encoding': 'base64'
            },
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            data: {
                keyword:key,
                scope:5,
                latitude:this.data.latitude,
                longitude:this.data.longitude
            },
            success: res => {
                console.log(res);
                if(res.statusCode==200){
                    let lists=res.data.data;
                    this.setData({
                        commodity:lists,
                        selectedMenu:"all"
                    });
                    this.setData({
                        displayList:this.data.commodity[this.data.selectedMenu],
                        markers:this.setMarker(this.data.commodity[this.data.selectedMenu])
                    });
                    if(lists.all.length==0){
                        swan.showModal({
                            title: '无数据',
                            content: '附近10公里没有找到相关结果呢'
                        });
                    }
                }else{
                    swan.showModal({
                        title:"服务器请求出错",
                        content:res.statusCode
                    });
                }
                
            },
            fail: err => {
                swan.showModal({
                    title: '错误信息',
                    content: JSON.stringify(err)
                });
                console.log(err);
            },
            complete: () => {

            }
        });
    },
    // 设置小程序tab标签的相关函数
    selectTab:function(e){
        this.setData({
            selectedMenu:e.detail.name,
            displayList:this.data.commodity[e.detail.name],
            markers:this.setMarker(this.data.commodity[e.detail.name])
        })
    },
    //设置结果列表的显示和隐藏
    goUp:function(e){
        this.setData({
            fullscreen:!this.data.fullscreen
        });
        console.log(this.data.fullscreen);
    },
    goto:function(e){
        swan.navigateTo({
            url:e.currentTarget.dataset.url
        })
    },
    //点击商品详情跳转到页面函数
    gotoDetail:function(e){
        console.log(e);
        swan.navigateTo({
            url:"/pages/detail/detail?goodid="+e.currentTarget.dataset.id
        });
    },
    // 地图功能相关函数
    // 点击marker之后
    showMsg(e){
        let goodid=e.markerId;
        console.log(goodid);
        swan.navigateTo({
            url: '/pages/detail/detail?goodid='+goodid
        });
    },
    setMarker(goods){
        let temp=[];
        for(let i=0;i<goods.length;i++){
            let marker={};
            marker["markerId"]=goods[i]["goodId"];
            marker["addr_name"]=goods[i]["marker"]["addr_name"];
            marker["address"]=goods[i]["marker"]["address"];
            marker["longitude"]=goods[i]["marker"]["longitude"];
            marker["latitude"]=goods[i]["marker"]["latitude"];
            marker["iconPath"]="../../static/images/location.png";
            temp.push(marker);
        }
        return temp;
    },

    onPullDownRefresh() {
        this.initdata();
    },
    onLoad(){
        swan.getSystemInfo({
            success: res => {
                this.setData({
                    systemInfo:res
                });
                if(res.windowHeight<550){
                    this.setData({
                        map_rate:0.5
                    })
                }
            }
        });
    },
    onReady(){
        swan.getLocation({
            type: 'gcj02',
            altitude: true,
            success: res => {
                this.setData({
                    latitude:res["latitude"],
                    longitude:res["longitude"]
                });
                this.initdata();
            },
            fail: err => {
                swan.showToast({
                    title: '获取失败,请检查位置授权是否开启',
                    icon: 'none'
                });
            }
        });
        this.mapContext=swan.createMapContext('myMap');   
    },
    initdata(){
        console.log(this.data.longitude);
        console.log(this.data.latitude);
        swan.request({
            url: fakedata.urls.getgoods,
            header: {
                'Content-Type': 'application/json',
                'Developer-Server-Domain': fakedata.host,
                'X-SP-Accept-Encoding': 'base64'
            },
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            data: {
                number:100,
                scope:10,
                latitude:this.data.latitude,
                longitude:this.data.longitude
            },
            success: res => {
                console.log(res);
                if(res.statusCode==200){
                    let lists=res.data.data;
                    this.setData({
                        commodity:lists,
                        selectedMenu:'all'
                    });
                    this.setData({
                        displayList:this.data.commodity[this.data.selectedMenu],
                        markers:this.setMarker(this.data.commodity[this.data.selectedMenu])
                    });
                    if(lists.all.length==0){
                        swan.showModal({
                            title: '没有结果',
                            content: '难为情的说，附近10公里内没有相关商品呢~'
                        });
                    }
                }else{
                    swan.showModal({
                        title:"服务器出错",
                        content:res
                    });
                }
            },
            fail: err => {
                swan.showModal({
                    title: '错误信息',
                    content: JSON.stringify(err)
                });
            },
            complete: () => {
            }
        });
    }
})